home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / gle / util / surf / vdevice.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-29  |  5.0 KB  |  210 lines

  1. /* Screen and .gle driver for pc */
  2. #include "all.h"
  3. #include "vdevice.h"
  4. #define false 0
  5. #define true (!false)
  6. #ifdef __TURBOC__
  7. #include <graphics.h>
  8. #else        /* define dummy routines so it will compile on vax */
  9. setcolor(){}
  10. setlinestyle(){}
  11. settextjustify(){}
  12. closegraph(){}
  13. detectgraph(){} 
  14. getmaxcolor(){} 
  15. outtext(){}
  16. getmaxx(){}
  17. getmaxy(){}
  18. grapherrormsg(){}
  19. graphresult(){ return 0;}
  20. initgraph(){}
  21. lineto(){}
  22. moveto(){}
  23. #define SOLID_LINE 1
  24. #define DASHED_LINE 2
  25. #endif
  26. #include <math.h>
  27. int ingraphmode;
  28. /*---------------------------------------------------------------------------*/
  29. #define pi 3.141592653
  30. #define true (!false)
  31. int getch(void);
  32. int kbhit(void);
  33. extern int gle_nspeed;
  34. extern int gle_speed;
  35. /*---------------------------------------------------------------------------*/
  36. /* The global variables for the PC screen driver */
  37. /*-----------------------------------------------*/
  38.  
  39. #define gerr() ee = graphresult(); if (ee!=0) printf("Graph error: %s \n",grapherrormsg(ee));
  40. #define textmode() {getch(); restorecrtmode();}
  41. #define graphmode() {getch(); setgraphmode(v_graphmode);}
  42.  
  43. float v_scale, v_xscale, v_yscale;
  44. #define xsizecm 21.0
  45. #define ysizecm 18.0
  46. #define sx(v) ( (int) ((v) * v_xscale)+1)
  47. #define sy(v) ( v_maxy - ((int) ((v) * v_yscale)))
  48. #define rx(v) ( (int) ((v) * v_xscale)+1)
  49. #define ry(v) ( v_maxy - ((int) ((v) * v_yscale)))
  50. char *gledir(char *s);
  51. int v_graphmode;
  52. int v_fillstyle=1,v_fillcolor;
  53. int vv_lstyle,v_lwidth;
  54. int v_maxy;
  55. static int ee;
  56. FILE *gf;
  57. extern char input_file[];
  58. char *bgidir();
  59. /*---------------------------------------------------------------------------*/
  60. v_open(float width, float height)
  61. {
  62.     static int g_driver,g_error;
  63.     float f;
  64.     char bigfile[80];
  65.     char glefile[80];
  66.  
  67.     strcpy(bigfile,input_file);
  68.     if (strchr(bigfile,'.')==NULL) {
  69.         gprint("Error in file name,  {%s} \n",input_file);
  70.         gle_abort("error in file name \n");
  71.     }
  72.     *strchr(bigfile,'.') = 0;
  73.     strcpy(glefile,bigfile);
  74.     strcat(glefile,".gle");
  75.     strcat(bigfile,".big");
  76.     gf = fopen(glefile,"r");
  77.     if (gf==NULL) { /* then it doesn't exist, so lets create it */
  78.         gf = fopen(glefile,"w");
  79.         if (gf==NULL) gprint("Failed to create {%s} \n",glefile);
  80.         else {
  81.             fprintf(gf,"size 24 18\n");
  82.             fprintf(gf,"bigfile %s\n",bigfile);
  83.             fclose(gf);
  84.         }
  85.     } else fclose(gf);
  86.  
  87.  
  88.     gf = fopen(bigfile,"w");
  89.     if (gf==NULL) {
  90.         printf("Unable to open output gle file out.gle\n");
  91.         abort();
  92.     }
  93.     gle_nspeed = 2;
  94. #ifdef __TURBOC__
  95.     detectgraph(&g_driver, &v_graphmode);
  96.     if (g_driver<0) {
  97.         printf("No graphics hardware detected !!!!! \n");
  98.         gle_abort("Could not load BGI graphics\n");
  99.     }
  100.     ingraphmode = true;
  101.     initgraph(&g_driver,&v_graphmode,bgidir());
  102.     g_error = graphresult();
  103.     if (g_error<0) {
  104.         printf("Init graph error %s\n",grapherrormsg(g_error));
  105.         ingraphmode = false; gle_abort("Init graph error\n");
  106.     }
  107.  
  108.     /* Get largest rectangle we can fit on the screen */
  109.     v_scale = xsizecm / width;
  110.     f = ysizecm / height;
  111.     if (f<v_scale) v_scale = f;
  112.  
  113.     v_xscale = v_scale * (getmaxx()-2) / xsizecm; /* Device Scale X, Device Scale y */
  114.     v_yscale = v_scale * (getmaxy()-22) / ysizecm;
  115.     v_maxy = getmaxy()-21;
  116.     setcolor(getmaxcolor());
  117.     setfillstyle(SOLID_FILL,getmaxcolor());
  118.     bar(1,1,getmaxx(),getmaxy());
  119.     setcolor(0);
  120. #endif
  121. }
  122. /*---------------------------------------------------------------------------*/
  123. v_close()
  124. {
  125.     ingraphmode = false;
  126. #ifdef __TURBOC__
  127.     text_inkey();
  128. #endif
  129.     closegraph();
  130.     fclose(gf);
  131. }
  132. /*---------------------------------------------------------------------------*/
  133. v_lstyle(char *s)
  134. {
  135.     if (*s == 0) s = "1";
  136.     vv_lstyle = DASHED_LINE;
  137.     if (strcmp(s,"")==0) vv_lstyle = SOLID_LINE;
  138.     if (strcmp(s,"1")==0) vv_lstyle = SOLID_LINE;
  139.     setlinestyle(vv_lstyle,0,v_lwidth);
  140.     fprintf(gf,"set lstyle %s\n",s);
  141.     gerr();
  142. }
  143. /*---------------------------------------------------------------------------*/
  144. v_line(float zx,float zy)
  145. {
  146. #ifndef unix
  147.     if (gle_speed) getch();
  148. #endif
  149.     lineto(sx(zx),sy(zy));
  150.     fprintf(gf,"aline %g %g \n",zx,zy);
  151. }
  152. v_move(float zx,float zy)
  153. {
  154.     moveto(sx(zx),sy(zy));
  155.     fprintf(gf,"amove %g %g \n",zx,zy);
  156. }
  157. /*---------------------------------------------------------------------------*/
  158. v_color(char *s)
  159. {
  160.     int i;
  161.     i = 0;
  162.     if (*s == 0) s = "BLACK";
  163.     if (strcmp(s,"RED")==0) i = 12;
  164.     if (strcmp(s,"BLUE")==0) i = 9;
  165.     if (strcmp(s,"GREEN")==0) i = 10;
  166.     if (strcmp(s,"YELLOW")==0) i = 14;
  167.     if (strcmp(s,"MAGENTA")==0) i = 5;
  168.     if (strcmp(s,"BLACK")==0) i = 0;
  169.     if (strcmp(s,"WHITE")==0) i = getmaxcolor();
  170.     if (i>=getmaxcolor()) i = 0;
  171.     setcolor(i);
  172.     fprintf(gf,"set color %s\n",s);
  173. }
  174. v_gsave()
  175. {
  176.     fprintf(gf,"gsave \n");
  177. }
  178. v_grestore()
  179. {
  180.     fprintf(gf,"grestore \n");
  181. }
  182. v_text(char *s)
  183. {
  184.     fprintf(gf,"text %s\n",s);
  185.     outtext(s);
  186. }
  187. v_set_hei(float g)
  188. {
  189.     fprintf(gf,"set hei %g\n",g);
  190. }
  191. v_set_just(char *s)
  192. {
  193.     if (strcmp(s,"RC")==0) settextjustify(2,0);
  194.     if (strcmp(s,"TC")==0) settextjustify(1,2);
  195.     if (strcmp(s,"BC")==0) settextjustify(1,0);
  196.     if (strcmp(s,"CC")==0) settextjustify(1,1);
  197.     if (strcmp(s,"LEFT")==0) settextjustify(0,0);
  198.     fprintf(gf,"set just %s\n",s);
  199. }
  200. v_rotate(float g)
  201. {
  202.     fprintf(gf,"rotate %g \n",g);
  203. }
  204. v_marker(char *m)
  205. {
  206.     settextjustify(1,1);
  207.     outtext("o");
  208.     fprintf(gf,"marker %s\n",m);
  209. }
  210.